define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wechat/applet/index',
                    add_url: 'wechat/applet/add',
                    edit_url: 'wechat/applet/edit',
                    del_url: 'wechat/applet/del',
                    multi_url: 'wechat/applet/multi',
                    table: 'wechat_applet',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'appletid', title: __('Appletid')},
                        {field: 'name', title: __('Name')},
                        {field: 'typedata_text', title: __('Typedata'), operate:false},
                        {field: 'appid', title: __('Appid')},
                        {field: 'mchid', title: __('Mchid')},
                        {field: 'state_text', title: __('State'), operate:false},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 切换应用
            $('#set_applet').click(function(){
                // 获取勾选的记录
                var ids = $.map(table.bootstrapTable('getSelections'), function (row) {
                    return row.id;
                });

                $.post("wechat/applet/set_applet",{
                    ids: ids
                },function(data){
                    if (data.code==1) {
                        Toastr.success(data.msg);
                        top.location.reload();
                    } else {
                        Toastr.error(data.msg);
                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});